package com.example.cushion.ui.loading;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.example.cushion.R;

import org.itzheng.and.baseutils.log.LogHelper;

/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2018-4-13.
 */
public class LoadingView extends FrameLayout implements ILoading {
    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 初始化布局
     */
    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_view_loading, this, true);
    }

    private static final String TAG = "LoadingView";

    @Override
    public void showLoading() {
        setVisibility(VISIBLE);
        LogHelper.d(TAG, "showLoading");
    }

    @Override
    public void dismissLoading() {
        setVisibility(GONE);
    }

    @Override
    public boolean isLoading() {
        return getVisibility() == VISIBLE;
    }

    /**
     * 将view添加到ViewGroup
     *
     * @param viewParent
     */
    public void attachToRoot(ViewGroup viewParent) {
        //判断是否已经有父窗体，如果有，则移除
        if (getParent() != null) ((ViewGroup) getParent()).removeView(this);
        //设置宽高为填充父窗体
        ViewGroup.LayoutParams params
                = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        viewParent.addView(this, params);
    }
}
